/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VSLangProj80;
using System.IO;

namespace DaveSexton.DocProject.Sandcastle
{
	public sealed partial class LocateCompilersControl : UserControl
	{
		#region Public Properties
		public string HtmlHelpWorkshopLocation
		{
			get
			{
				return help1TextBox.Text;
			}
			set
			{
				help1TextBox.Text = value;
			}
		}

		public bool IsHtmlHelpWorkshopLocationValid
		{
			get
			{
				try
				{
					return !string.IsNullOrEmpty(help1TextBox.Text) && File.Exists(help1TextBox.Text);
				}
				catch (IOException)
				{
					return false;
				}
			}
		}
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="LocateCompilersControl" /> class.
		/// </summary>
		public LocateCompilersControl()
		{
			InitializeComponent();

			help1TextBox.Text = SandcastleBuildEngineProvider.Paths.HtmlHelpWorkshop;
		}
		#endregion

		#region Events
		private readonly object HelpLocationChangedEvent = new object();
		
		/// <summary>
		/// Event raised after one of the <see cref="HtmlHelpWorkhsopLocation" /> or <see cref="HelpVersion2Location"/> properties' values have changed.
		/// </summary>
		[Category("Property Changed")]
		[Description("Event raised after one of the HtmlHelpWorkshopLocation or HelpVersion2Location properties' values have changed.")]
		public event EventHandler HelpLocationChanged
		{
			add
			{
				lock (HelpLocationChangedEvent)
				{
					Events.AddHandler(HelpLocationChangedEvent, value);
				}
			}
			remove
			{
				lock (HelpLocationChangedEvent)
				{
					Events.RemoveHandler(HelpLocationChangedEvent, value);
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="HelpLocationChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
		private void OnHelpLocationChanged(EventArgs e)
		{
			EventHandler handler = null;

			lock (HelpLocationChangedEvent)
			{
				handler = (EventHandler) Events[HelpLocationChangedEvent];
			}

			if (handler != null)
				handler(this, e);
		}
		#endregion

		#region Event Handlers
		private void helpLocation_TextChanged(object sender, EventArgs e)
		{
			OnHelpLocationChanged(e);

			Control control = (Control) sender;

			if (!IsHtmlHelpWorkshopLocationValid)
				errorProvider1.SetError(control, "Invalid file specified.");
			else
				errorProvider1.SetError(control, null);
		}

		private void helpLocation_Enter(object sender, EventArgs e)
		{
			((TextBox) sender).SelectAll();
		}

		private void help1BrowseButton_Click(object sender, EventArgs e)
		{
			string dir = help1TextBox.Text;
			
			if (!string.IsNullOrEmpty(dir))
			{
				try
				{
					dir = Path.GetDirectoryName(dir);
				}
				catch (ArgumentException)
				{
					dir = null;
				}
			}
			
			if (string.IsNullOrEmpty(dir))
				dir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

			using (OpenFileDialog dialog = new OpenFileDialog())
			{
				dialog.CheckFileExists = true;
				dialog.CheckPathExists = true;
				dialog.Filter = "Html Help Workshop (hhc.exe)|hhc.exe|All Programs (*.exe)|*.exe";
				dialog.InitialDirectory = dir;
				dialog.Multiselect = false;
				dialog.Title = "Find Html Help Workshop Compiler";

				if (dialog.ShowDialog(DocProjectEnvironment.MainWindow) == DialogResult.OK)
					help1TextBox.Text = dialog.FileName;
			}
		}

		private void downloadHelp1xLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start(Resources.Text.DownloadHelp1xLink);
		}
		#endregion
	}
}
